package oa.lyk.service.Personresource;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oa.dao.MsgDao;
import oa.dao.SalaryDao;
import oa.lyk.service.Personoffice.Page;
import oa.pojo.Salary;
import oa.view.SalaryView;

import com.google.gson.Gson;

public class SalaryManager {
	public String query(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String strCurrentPage = request.getParameter("currPage");
		String name=request.getParameter("username");
		if(name != null ){
			try {
				name = new String(name .getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		int currentPage = 1;
		try{
			currentPage=Integer.parseInt(strCurrentPage);
		}catch (Exception e){
			currentPage=1;
		}
		SalaryDao sDao = new SalaryDao();
		request.setAttribute("salarypage", sDao.query(currentPage, name));
		return "querySalaryList";
	}
	
	public String delete(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String id = request.getParameter("id");
		int currPage = Integer.parseInt(request.getParameter("currPage"));
		request.setAttribute("currPage", currPage);
		SalaryDao sDao = new SalaryDao();
		sDao.del(id);
		
		return "delSalary";
	}
	
	public String look(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String id = request.getParameter("id");
		String currPage = request.getParameter("currPage");
		SalaryDao sDao = new SalaryDao();
		SalaryView sView =sDao.search(id);
		request.setAttribute("sView", sView);
		request.setAttribute("currPage", currPage);
		return "lookSalary";
	}
	
	public String update(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String currPage = request.getParameter("currPage");
		String deptname = request.getParameter("deptname");
		String salid = request.getParameter("salid");
		String userid = request.getParameter("userid");
		String job = request.getParameter("job");
		String salary = request.getParameter("salary");
		String bouns = request.getParameter("bouns");
		String salFine = request.getParameter("salFine");
		Salary sal = new Salary();
		sal.setSalId(salid);
		sal.setBaseSalary(salary);
		sal.setBonus(bouns);
		sal.setDeptName(deptname);
		sal.setSalFine(salFine);
		sal.setUserId(userid);
		sal.setStuffJob(job);
		
		SalaryDao sDao = new SalaryDao();
		sDao.updateById(sal);
		request.setAttribute("currPage", currPage);
		return "updateSalary";
	}
	
	public String now(HttpServletRequest req,HttpServletResponse res){
		return "now";
	}
	
	public void ajaxquery(HttpServletRequest request,HttpServletResponse respnse){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String strCurrentPage = request.getParameter("currPage");
		int currentPage = 1;
		try{
			currentPage=Integer.parseInt(strCurrentPage);
		}catch (Exception e){
			currentPage=1;
		}
		MsgDao mDao = new MsgDao();
		Page msg = mDao.query(currentPage,null);
		Gson gson=new Gson();
		String jsonStr=gson.toJson(msg);
		respnse.setContentType("text/json;charset=utf-8");
		PrintWriter pw;
		try {
			pw = respnse.getWriter();
			pw.print(jsonStr);
			pw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String save(HttpServletRequest request,HttpServletResponse respnse){
		return "saveSalary";
	}
}
